﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CoolCode.Web.UI
{
    public  class ControlBinder:IControlBinder
    {  
        public virtual bool UpdateControl(Control container, Dictionary<string, object> valueProvider, PrefixType _prefixType)
        {
            var cs = GetControls(container);
            foreach (var c in cs)
            {
                string key = MakeupControlId(c.ID, _prefixType);
                if (key == null) continue;
                key = key.ToUpper();
                if (valueProvider.ContainsKey(key))
                {
                    object value =valueProvider[key];
                    string text = ConvertToString(value);
                    if (c is TextBox)
                        ((TextBox)c).Text =text  ;
                    else if (c is HiddenField)
                        ((HiddenField)c).Value =text ;
                    //else if( c is DateTime ) 
                    //else if (c is CheckBox )
                    //    ((CheckBox)c).Checked = valueProvider[key] ;
                }
            }

            var labels = cs.Where(c => c is Label && c.ID.Length > 6 && c.ID.StartsWith("lbldsp", StringComparison.OrdinalIgnoreCase)).Cast<Label>();
            foreach (var c in labels)
            {
                string key = c.ID.Substring(6).ToUpper();
                if (valueProvider.ContainsKey(key))
                {
                    c.Text =ConvertToString( valueProvider[key]);
                }
            }
            return true ;
        }

        public virtual  List<Control> GetControls(Control container)
        {
            return container.GetControls();
        }

        private string ConvertToString(object value)
        {
            return value ==null ? string.Empty :value.ToString();
        }
        
       protected virtual  string  MakeupControlId(string key, PrefixType _prefixType)
        {            
                switch (_prefixType)
                {
                    case PrefixType.None:
                        break;
                    case PrefixType.Camel:
                        int i;
                        for (i = 0; i < key.Length; i++)
                        {
                            if (key[i] > 'z' || key[i] < 'a')
                                break;
                        }
                        if (i > 0)
                        {
                            key = key.Substring(i);
                        }
                        else
                        {
                            return null;
                        }
                        break;
                    case PrefixType.Three:
                        if (key.Length > 3)
                        {
                            key = key.Substring(3);
                        }
                        break;
                }
                return key;
        }


       public  void InitialValue(Control control)
       {
           List<Control> cs =GetControls( control);
           InitialValue(cs);
       }

       protected virtual void InitialValue(List<Control> cs)
       {
           foreach (var c in cs)
           {
               if (c is Label)
               {
                   Label cc = (Label)c;
                   if (cc.ID.ToUpper().StartsWith("LBLDSP"))
                       cc.Text = string.Empty;
               }
               if (c is TextBox)
               {
                   TextBox cc = (TextBox)c;
                   cc.Text = string.Empty;
               }
               if (c is DropDownList)
               {
                   DropDownList cc = (DropDownList)c;
                   cc.SelectedIndex = -1;
               }
               else if (c is CheckBox)
               {
                   CheckBox cc = (CheckBox)c;
                   cc.Checked = false;
               }
           }
       }
    }

    public interface IControlBinder
    {
        bool UpdateControl(Control container, Dictionary<string, object> valueProvider, PrefixType _prefixType);
        void InitialValue(Control container);
    }

    public interface IControlBindable
    {
        IControlBinder ControlBinder { get; }
    }
}
